﻿using Flare.DB.CeEntity;
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;

namespace Flare.Pages.General
{
    public partial class Department : UserControl, ITypicalPage
    {
        public string name
        {
            get { return "Отделы"; }
        }

        public Department()
        {
            InitializeComponent();
        }

        private void ButtonAdd_Click(object sender, RoutedEventArgs e)
        {
            var data = new general_department()
            {
                department = department.Text,
                department_address = department_address.Text,
                department_contact = department_contact.Text,
                department_contact_phone = department_contact_phone.Text
            };

            try
            {
                new DB.general_departments().Create(data);
                DepartmentsList.SelectedItem = null;
            }
            catch (Exception ex) {
                glob.kernel.ShowNotice(ex.Message);
            }
            (this.TryFindResource("general_departmentsDataSet") as ObjectDataProvider).Refresh();
        }

        private void DepartmentsList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (DepartmentsList.SelectedIndex == -1)
            {
                ButtonSave.Visibility = System.Windows.Visibility.Collapsed;
                ButtonDelete.Visibility = System.Windows.Visibility.Collapsed;
                ButtonClose.Visibility = System.Windows.Visibility.Collapsed;
                ButtonAdd.Visibility = System.Windows.Visibility.Visible;
            }
            else
            {
                ButtonSave.Visibility = System.Windows.Visibility.Visible;
                ButtonDelete.Visibility = System.Windows.Visibility.Visible;
                ButtonClose.Visibility = System.Windows.Visibility.Visible;
                ButtonAdd.Visibility = System.Windows.Visibility.Collapsed;
            }
        }

        private void ButtonSave_Click(object sender, RoutedEventArgs e)
        {
            if (DepartmentsList.SelectedIndex == -1) return;

            var data = new general_department()
            {
                id_department = (int)DepartmentsList.SelectedValue,
                department = department.Text,
                department_address = department_address.Text,
                department_contact = department_contact.Text,
                department_contact_phone = department_contact_phone.Text
            };

            try
            {
                new DB.general_departments().Edit(data);
                DepartmentsList.SelectedItem = null;
            }
            catch (Exception ex)
            {
                glob.kernel.ShowNotice(ex.Message);
            }
            (this.TryFindResource("general_departmentsDataSet") as ObjectDataProvider).Refresh();
        }

        private void ButtonDelete_Click(object sender, RoutedEventArgs e)
        {
            if (DepartmentsList.SelectedIndex == -1) return;
            try
            {
                new DB.general_departments().Delete((int)DepartmentsList.SelectedValue);
                DepartmentsList.SelectedItem = null;
            }
            catch (Exception ex)
            {
                glob.kernel.ShowNotice(ex.Message);
            }
            (this.TryFindResource("general_departmentsDataSet") as ObjectDataProvider).Refresh();
        }

        private void ButtonClose_Click(object sender, RoutedEventArgs e)
        {
            DepartmentsList.SelectedItem = null;
        }


    }
}
